package newcrunch.gui.multispantable;

// Originally from http://www.java2s.com/Code/Java/Swing-Components/MultiSpanCellTableExample.htm
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;




/**
 * @version 1.0 11/26/98
 */
public class MultiSpanCellTableExample extends JFrame {

  MultiSpanCellTableExample() {
    super( "Multi-Span Cell Example" );
    
    AttributiveCellTableModel ml = new AttributiveCellTableModel(3,3);
    /*
    AttributiveCellTableModel ml = new AttributiveCellTableModel(10,6) {
      public Object getValueAt(int row, int col) { 
        return "" + row + ","+ col; 
      }
    };
    */
//    ml.setEditable(0, 0, false);
    ColoredCell colored = (ColoredCell)ml.getCellAttribute();
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
        colored.setBackground(Color.yellow, i, j);
    ml.setValueAt("test", 0, 0);
    
    final CellSpan cellAtt =(CellSpan)ml.getCellAttribute();
    final MultiSpanCellTable table = new MultiSpanCellTable( ml );
    
    table.setHorizontalAlignment(SwingConstants.CENTER);
    cellAtt.combine(new int[]{0, 1}, new int[]{0});
    
    JScrollPane scroll = new JScrollPane( table );

    JButton b_one   = new JButton("Combine");
    b_one.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
  int[] columns = table.getSelectedColumns();
  int[] rows    = table.getSelectedRows();
  cellAtt.combine(rows,columns);
  table.clearSelection();
  table.revalidate();
  table.repaint();
      }
    });
    JButton b_split = new JButton("Split");
    b_split.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
  int column = table.getSelectedColumn();
  int row    = table.getSelectedRow();
  cellAtt.split(row,column);
  table.clearSelection();
  table.revalidate();
  table.repaint();
      }
    });
    JPanel p_buttons = new JPanel();
    p_buttons.setLayout(new GridLayout(2,1));
    p_buttons.add(b_one);
    p_buttons.add(b_split);

    Box box = new Box(BoxLayout.X_AXIS);
//    box.add(scroll);
    box.add(table);
    box.add(new JSeparator(SwingConstants.HORIZONTAL));
    box.add(p_buttons);
    getContentPane().add( box );
    setSize( 400, 200 );
    setVisible(true);
  }

  public static void main(String[] args) {
    MultiSpanCellTableExample frame = new MultiSpanCellTableExample();
    frame.addWindowListener( new WindowAdapter() {
      public void windowClosing( WindowEvent e ) {
        System.exit(0);
      }
    });
  }
}















